using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VHSDAT
    {
        public DataRow Convert2HSDAT(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryHSDAT(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["HSMANDANT"] = " ".PadRight(2 ); 
         dwReturn["HSSA"] = " ".PadRight(4 ); 
         dwReturn["HSSL1"] = " ".PadRight(20 ); 
         dwReturn["HSSL2"] = " ".PadRight(20 ); 
         dwReturn["HSSL3"] = " ".PadRight(20 ); 
         dwReturn["HSSL4"] = " ".PadRight(20 ); 
         dwReturn["HSSPRKZ"] = " ".PadRight(1 ); 
         dwReturn["HSNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["HSNEUDAT"] = " ".PadRight(0 ); 
         dwReturn["HSAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["HSAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["HSAENDZL"] = " ".PadRight(0 ); 
         dwReturn["HSHE"] = " ".PadRight(1 ); 
         dwReturn["HSSTATUS"] = " ".PadRight(1 ); 
         dwReturn["HSBEZ"] = " ".PadRight(50 ); 
         dwReturn["HSKBEZ"] = " ".PadRight(10 ); 
         dwReturn["HSWERT1"] = " ".PadRight(20 ); 
         dwReturn["HSWERT2"] = " ".PadRight(20 ); 
         dwReturn["HSWERT3"] = " ".PadRight(20 ); 
         dwReturn["HSWERT4"] = " ".PadRight(20 ); 
         dwReturn["HSWERT5"] = " ".PadRight(20 ); 
         dwReturn["HSWERTN"] = " ".PadRight(0 ); 
         dwReturn["HSFILLER"] = " ".PadRight(21 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapHSDAT(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveHSDAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryHSDAT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapHSDAT(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertHSDAT(dwLocal, ITXDBVERSION);
            }
            UpdateHSDAT(dwLocal);
        }
        public void InsertHSDAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO HSDAT(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryHSDAT(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM HSDAT WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM HSDAT  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "HSDAT";
            Return = Result;
            return Return;
        }
        public void DeleteHSDAT(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM HSDAT   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateHSDAT(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE HSDAT SET "
                    +"HSMANDANT= :HSMANDANT,"
                    +"HSSA= :HSSA,"
                    +"HSSL1= :HSSL1,"
                    +"HSSL2= :HSSL2,"
                    +"HSSL3= :HSSL3,"
                    +"HSSL4= :HSSL4,"
                    +"HSSPRKZ= :HSSPRKZ,"
                    +"HSNEUBEN= :HSNEUBEN,"
                    +"HSNEUDAT= :HSNEUDAT,"
                    +"HSAENDBEN= :HSAENDBEN,"
                    +"HSAENDDAT= :HSAENDDAT,"
                    +"HSAENDZL= :HSAENDZL,"
                    +"HSHE= :HSHE,"
                    +"HSSTATUS= :HSSTATUS,"
                    +"HSBEZ= :HSBEZ,"
                    +"HSKBEZ= :HSKBEZ,"
                    +"HSWERT1= :HSWERT1,"
                    +"HSWERT2= :HSWERT2,"
                    +"HSWERT3= :HSWERT3,"
                    +"HSWERT4= :HSWERT4,"
                    +"HSWERT5= :HSWERT5,"
                    +"HSWERTN= :HSWERTN,"
                    +"HSFILLER= :HSFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("HSMANDANT", OracleType.NVarChar).Value = dwData["HSMANDANT"].ToString(); 
          or1.Parameters.Add("HSSA", OracleType.NVarChar).Value = dwData["HSSA"].ToString(); 
          or1.Parameters.Add("HSSL1", OracleType.NVarChar).Value = dwData["HSSL1"].ToString(); 
          or1.Parameters.Add("HSSL2", OracleType.NVarChar).Value = dwData["HSSL2"].ToString(); 
          or1.Parameters.Add("HSSL3", OracleType.NVarChar).Value = dwData["HSSL3"].ToString(); 
          or1.Parameters.Add("HSSL4", OracleType.NVarChar).Value = dwData["HSSL4"].ToString(); 
          or1.Parameters.Add("HSSPRKZ", OracleType.NVarChar).Value = dwData["HSSPRKZ"].ToString(); 
          or1.Parameters.Add("HSNEUBEN", OracleType.NVarChar).Value = dwData["HSNEUBEN"].ToString(); 
          or1.Parameters.Add("HSNEUDAT", OracleType.NVarChar).Value = dwData["HSNEUDAT"].ToString(); 
          or1.Parameters.Add("HSAENDBEN", OracleType.NVarChar).Value = dwData["HSAENDBEN"].ToString(); 
          or1.Parameters.Add("HSAENDDAT", OracleType.NVarChar).Value = dwData["HSAENDDAT"].ToString(); 
          or1.Parameters.Add("HSAENDZL", OracleType.NVarChar).Value = dwData["HSAENDZL"].ToString(); 
          or1.Parameters.Add("HSHE", OracleType.NVarChar).Value = dwData["HSHE"].ToString(); 
          or1.Parameters.Add("HSSTATUS", OracleType.NVarChar).Value = dwData["HSSTATUS"].ToString(); 
          or1.Parameters.Add("HSBEZ", OracleType.NVarChar).Value = dwData["HSBEZ"].ToString(); 
          or1.Parameters.Add("HSKBEZ", OracleType.NVarChar).Value = dwData["HSKBEZ"].ToString(); 
          or1.Parameters.Add("HSWERT1", OracleType.NVarChar).Value = dwData["HSWERT1"].ToString(); 
          or1.Parameters.Add("HSWERT2", OracleType.NVarChar).Value = dwData["HSWERT2"].ToString(); 
          or1.Parameters.Add("HSWERT3", OracleType.NVarChar).Value = dwData["HSWERT3"].ToString(); 
          or1.Parameters.Add("HSWERT4", OracleType.NVarChar).Value = dwData["HSWERT4"].ToString(); 
          or1.Parameters.Add("HSWERT5", OracleType.NVarChar).Value = dwData["HSWERT5"].ToString(); 
          or1.Parameters.Add("HSWERTN", OracleType.NVarChar).Value = dwData["HSWERTN"].ToString(); 
          or1.Parameters.Add("HSFILLER", OracleType.NVarChar).Value = dwData["HSFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
